package com.hd.pzhwlxt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hd.pzhwlxt.mapper.MaterialPurchaseMapper;
import com.hd.pzhwlxt.pojo.Material;
import com.hd.pzhwlxt.pojo.MaterialPurchase;
import com.hd.pzhwlxt.pojo.Materialprice;
import com.hd.pzhwlxt.service.MaterialPurchaseService;
import com.hd.pzhwlxt.service.MaterialService;
import com.hd.pzhwlxt.service.MaterialpriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
/**
@create: 2022-09-26 11:35
@author: 怀念天空
@Description: 
*/

@Service
public class MaterialPurchaseServiceImpl extends ServiceImpl<MaterialPurchaseMapper, MaterialPurchase> implements MaterialPurchaseService{
    @Autowired
    private MaterialPurchaseMapper materialPurchaseMapper;

    @Autowired
    private MaterialService materialService;

    @Autowired
    private MaterialpriceService materialpriceService;

    @Override
    public List<MaterialPurchase> getAllPurchaseInfo() {
        return materialPurchaseMapper.getAllPurchaseInfo();
    }

    @Override
    public Page<MaterialPurchase> getAllPurchaseInfoPage(Page<Object> page) {
        return materialPurchaseMapper.getAllPurchaseInfoPage(page);
    }

    @Override
    public boolean addPurchaseInfo(MaterialPurchase materialPurchase) {
        Integer materialId = materialPurchase.getMaterialId();
        QueryWrapper<Materialprice> wrapper = new QueryWrapper<>();
        wrapper.eq("material_id",materialId);
        Materialprice materialprice = materialpriceService.getOne(wrapper);
        Double inPrice = materialprice.getInPrice();
        materialPurchase.setMoney(BigDecimal.valueOf(inPrice * materialPurchase.getPurchaseNum()));
        return materialPurchaseMapper.insert(materialPurchase) > 0;
    }

    @Override
    public boolean updatePurchaseInfo(MaterialPurchase materialPurchase) {
        Material material = materialService.getMaterialById(materialPurchase.getMaterialId());
        material.setStock(material.getStock() + materialPurchase.getPurchaseNum());
        materialService.updateById(material);
        materialPurchase.setStatus(1);
        return materialPurchaseMapper.updateById(materialPurchase) > 0;
    }
}
